package com.flesler.stickjoy.controller
{
	public final class Controller
	{
		private var _key:String;
		private var map:Object = {};
		
		public function Controller(key:String)
		{
			_key = key;
		}

		public function get key():String
		{
			return _key;
		}
		
		// CRUD of components
		
		public function add(component:ControllerComponent):void
		{
			map[component.key] = component;
		}
		
		public function has(component:ControllerComponent):Boolean
		{
			return component.key in map;
		}
		
		public function remove(component:ControllerComponent):void
		{
			delete map[component.key];
		}
		
		// Component getters
		
		public function getComponent(key:String):ControllerComponent
		{
			return map[key];
		}
		
		public function get components():Array
		{
			var list:Array = [];
			for each(var component:ControllerComponent in map)
				list.push(component);
			return list;
		}
		
		public function getButton(key:String):ControllerButton
		{
			return getComponent(key) as ControllerButton;
		}
		
		public function getAxis(key:String):ControllerAxis
		{
			return getComponent(key) as ControllerAxis;
		}
		
		// Shortcuts for buttons
		
		public function isDown(key:String):Boolean
		{
			return getButton(key).isDown;
		}
		
		public function press(key:String):void
		{
			return getButton(key).press();
		}
		
		public function release(key:String):void
		{
			return getButton(key).release();
		}
		
		// Shortcuts for axes
		
		public function getPosition(key:String):Number
		{
			return getAxis(key).position;
		}
		
		public function negative(key:String):void
		{
			return getAxis(key).negative();
		}
		
		public function center(key:String):void
		{
			return getAxis(key).center();
		}
		
		public function positive(key:String):void
		{
			return getAxis(key).positive();
		}
		
		// Destroyable implementation
		
		public function destroy():void
		{
			for each(var component:ControllerComponent in map)
				component.destroy();
			map = null;
		}
	}
}